﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Post.BBS
{
    public partial class Detail : Base.BasePage
    {
        public LabMS.Model.Post post;
        public DataSet dsReplies = new DataSet();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string id = Request.QueryString["postID"];
                if (string.IsNullOrEmpty(id)) { throw new Exception("对不起,您所访问的页面不存在"); }

                try { int postID = int.Parse(id); }
                catch { throw new Exception("对不起,您所访问的页面不存在"); }

                ExtendBLL.Post p = new ExtendBLL.Post();

                p.IncrementViewTimes(int.Parse(id));

                GetPost();
                GetReplies();
            }
        }

        protected void Page_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();
            WriteBackScript("alert('" + ex.Message + "')");
        }

        /// <summary>
        /// 获取帖子信息
        /// </summary>
        protected void GetPost()
        {
            int postID = int.Parse(Request.QueryString["postID"]);

            ExtendBLL.Post p = new ExtendBLL.Post();

            post = p.GetModel(postID);

            if (post == null) { WriteBackScript("alert('当前主题已经被删除!');window.location.href='Index.aspx';"); }
        }

        protected void GetReplies()
        {
            int postID = int.Parse(Request.QueryString["postID"]);

            LabMS.BLL.PostReply postReply = new LabMS.BLL.PostReply();

            DataSet ds = postReply.GetList(" [PosterID] = " + postID);

            AspNetPager.RecordCount = ds.Tables[0].Rows.Count;

            HandleReply(ds);
        }


        /// <summary>
        /// 处理回复
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        protected void HandleReply(DataSet ds)
        {
            dsReplies.Tables.Add("replies");
            dsReplies.Tables[0].Columns.AddRange(new DataColumn[] { 
                new DataColumn("ID"),
                new DataColumn("PostReplyContent"),
                new DataColumn("PosterID"),
                new DataColumn("PostReplyTime"),
                new DataColumn("ReplierID"),
                new DataColumn("ReplierType"),
                new DataColumn("ReplierName"),
                new DataColumn("PostReplySN")
            });

            DataRow row;
            int count = ds.Tables[0].Rows.Count;
            int current = AspNetPager.PageSize * (AspNetPager.CurrentPageIndex - 1) + 1;

            ExtendBLL.Post post = new ExtendBLL.Post();
            string replierName = String.Empty;

            for (int i = current - 1; (i < current + AspNetPager.PageSize - 1) && (i < count); i++)
            {
                row = ds.Tables[0].Rows[i];

                replierName = post.GetAuthorName(int.Parse(row["ReplierID"].ToString()), row["ReplierType"].ToString());

                dsReplies.Tables[0].Rows.Add(
                    row["ID"],
                    row["PostReplyContent"],
                    row["PosterID"],
                    row["PostReplyTime"],
                    row["ReplierID"],
                    row["ReplierType"],
                    string.IsNullOrEmpty(replierName) ? "<del>已删除用户</del>" : replierName,
                    i + 1
                );
            }
        }

        /// <summary>
        /// 写回脚本
        /// </summary>
        /// <param name="script">脚本内容</param>
        protected void WriteBackScript(string script)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                Guid.NewGuid().ToString(), "<script type='text/javascript'>" + script + "</script>");
        }

        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            GetPost();
            GetReplies();
        }

        /// <summary>
        /// 新增回复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnReply_Click(object sender, EventArgs e)
        {
            int postID = int.Parse(Request.QueryString["postID"]);

            LabMS.BLL.PostReply postReply = new LabMS.BLL.PostReply();
            LabMS.Model.PostReply postReplyInfo = new LabMS.Model.PostReply();

            postReplyInfo.ReplierID = int.Parse(Session["UserID"].ToString());
            postReplyInfo.ReplierType = Base.PrivilegeManager.GetUserTypeCode();

            postReplyInfo.PosterID = postID;
            postReplyInfo.PostReplyContent = Server.HtmlEncode(ReplyNew.Text);
            postReplyInfo.PostReplyTime = DateTime.Now;

            postReply.Add(postReplyInfo);

            // 绑定回复
            GetPost();
            GetReplies();

            ReplyNew.Text = "";
        }
    }
}
